import pbWFCMsg from '../proto/WFCMessage_pb'
import store from '../store/index'

// conack 连接成功返回payload
export const _connectAckPayload = (data) => {
    // 反序列化
    let connectAckPayload = pbWFCMsg.ConnectAckPayload.deserializeBinary(data)
    let obj = {
        msg_head: connectAckPayload.getMsgHead()
    }
    return obj
}

// set uid
export const _userRequest = (uid) => {
        const userRequest = new pbWFCMsg.UserRequest()
            // set值
        userRequest.setUid(uid)

        return userRequest
    }
    // 获取信息
export const _pullUserRequest = (arrUid) => {
    const pullUserRequest = new pbWFCMsg.PullUserRequest()
        // set值
    pullUserRequest.setRequestList(arrUid)
        // 序列化
    let bytes = pullUserRequest.serializeBinary()
    return bytes
}

// 用户信息
export const _pullUserResult = (data) => {
    // 反序列化
    let pullUserResult = pbWFCMsg.PullUserResult.deserializeBinary(data)

    return pullUserResult
}

// 用户信息
export const _userResult = (data) => {
    let arr = _pullUserResult(data).getResultList()
    let list = []
    for (var i = 0; i < arr.length; i++) {
        list.push(_user(arr[i].getUser()))
    }
    return list
}


// 用户user
export const _user = (user) => {
    let info = {
        uid: user.getUid(),
        name: user.getName(),
        address: user.getAddress(),
        company: user.getCompany(),
        countrycode: user.getCountrycode(),
        displayName: user.getDisplayName(),
        email: user.getEmail(),
        extra: user.getExtra(),
        gender: user.getGender(),
        mobile: user.getMobile(),
        portrait: user.getPortrait(),
        social: user.getSocial(),
        type: user.getType(),
        updateDt: user.getUpdateDt()
    }
    return info
}

// 获取通讯录
export const _version = (date) => {
    const version = new pbWFCMsg.Version()
        // set值
    version.setVersion(date)
        // 序列化
    let bytes = version.serializeBinary()
    return bytes
}



// 通讯录
export const _getFriendsResult = (data) => {
    // 反序列化
    let getFriendsResult = pbWFCMsg.GetFriendsResult.deserializeBinary(data)
    let arr = getFriendsResult.getEntryList()
    let list = []
    for (var i = 0; i < arr.length; i++) {
        list.push(_friend(arr[i]))
    }
    return list
}

export const _friend = (evt) => {
    let info = {
        uid: evt.getUid(),
        state: evt.getState(),
        update_dt: evt.getUpdateDt(),
        alias: evt.getAlias()
    }
    return info
}

// 发送消息
export const _message = (parm) => {
    let message = new pbWFCMsg.Message()
        // set
    message.setConversation(parm.conversation)
    message.setContent(parm.content)
    message.setFromUser(parm.fromUser) //userId
        // 序列化
    let bytes = message.serializeBinary()
    return bytes
}


export const _conversation = (parm) => {
    let conversation = new pbWFCMsg.Conversation()

    conversation.setType(parm.type) //0 私聊 1群聊
    conversation.setTarget(parm.target) //对方uid
    conversation.setLine(parm.line)

    return conversation
}


export const _messageContent = (parm) => {
    let messageContent = new pbWFCMsg.MessageContent()
    messageContent.setType(parm.type)
	messageContent.setSearchableContent(parm.content) //聊天内容
	if(parm.type == 3){
		messageContent.setMediatype(parm.mediaType)  // 媒体type
		messageContent.setRemotemediaurl(parm.remoteMediaUrl) //地址
		messageContent.setData(parm.data)
	}
	console.log(messageContent)
    return messageContent
}

// MN
export const _notifyMessage = (data) => {
    // 反序列化
    let notifyMessage = pbWFCMsg.NotifyMessage.deserializeBinary(data)

    let obj = {
        type: notifyMessage.getType(),
        head: notifyMessage.getHead(),
        target: notifyMessage.getTarget()
    }
    return obj
}


// MP
export const _pullMessageRequest = (parm) => {
    let pullMessageRequest = new pbWFCMsg.PullMessageRequest()
        // set
    pullMessageRequest.setId(parm.id)
    pullMessageRequest.setType(parm.type)
        // 序列化
    let bytes = pullMessageRequest.serializeBinary()
    return bytes
}

// MP 回包
export const _pullMessageResult = (data) => {
    // 反序列化
    let pullMessageResult = pbWFCMsg.PullMessageResult.deserializeBinary(data)
        // get
    let obj = {
        head: pullMessageResult.getHead().toString(),
        messageList: pullMessageResult.getMessageList(),
        current: pullMessageResult.getCurrent()
	}
    return obj
}

// msg res
export const _messageRes = (data) => {
    let obj = {
        conversation: _conversationRes(data.getConversation()),
        from_user: data.getFromUser(),
        content: _messageContentRes(data.getContent()),
        server_timestamp: data.getServerTimestamp(),
        to_list: data.getToList(),
        to_user: data.getToUser()
    }
    return obj
}

// 
export const _conversationRes = (data) => {
    let obj = {
        type: data.getType(),
        target: data.getTarget(),
        line: data.getLine()
    }
    return obj
}

// 
export const _messageContentRes = (data) => {
    let obj = {
        type: data.getType(),
        content: data.getSearchableContent(),
        remoteMediaUrl: data.getRemotemediaurl(),
        data: data.getData()
    }
    return obj
}

// 图片
export const _getUploadTokenResult = (data) => {
    // 反序列化
    let result = pbWFCMsg.GetUploadTokenResult.deserializeBinary(data)
    let obj = {
        domain: result.getDomain(),
        token: result.getToken(),
        server: result.getServer(),
        port: result.getPort()
    }
    return obj
}

export const _pullGroupInfoResult = (data) => {
    // 反序列化
    let result = pbWFCMsg.PullGroupInfoResult.deserializeBinary(data)
	return result
}

// 群信息
export const _groupInfoRes = (data) => {
	let obj = {
		extra: data.getExtra(),
		joinType: data.getJoinType(),
		memberCount: data.getMemberCount(),
		memberUpdateDt: data.getMemberUpdateDt(),
		mute: data.getMute(),
		name: data.getName(),
		owner: data.getOwner(),
		portrait: data.getPortrait(),
		privateChat: data.getPrivateChat(),
		proclamation: data.getProclamation(),
		searchable: data.getSearchable(),
		targetId: data.getTargetId(),
		type: data.getType(),
		updateDt: data.getUpdateDt()
	}
	return obj
}

// 创建群
export const _createGroupRequest = (parm) => {
    let createGroupRequest = new pbWFCMsg.CreateGroupRequest()
        // set
    createGroupRequest.setGroup(parm.group)
    createGroupRequest.setToLineList(0)
        // 序列化
    let bytes = createGroupRequest.serializeBinary()
    return bytes
}

// group
export const _group = (parm) => {
    let group = new pbWFCMsg.Group()
        // set
    group.setGroupInfo(parm.groupInfo)
    group.setMembersList(parm.groupMember)
    return group
}

// groupinfo
export const _groupInfo = (parm) => {
    let groupInfo = new pbWFCMsg.GroupInfo()
        // set
    groupInfo.setName(parm.name)
    groupInfo.setType(0)
    groupInfo.setSearchable(0)
    groupInfo.setJoinType(0)
    groupInfo.setPrivateChat(0)
    groupInfo.setMute(0)
    return groupInfo
}

// 获取群成员
export const _pullGroupMemberRequest = (parm) => {
    let pullGroupMemberRequest = new pbWFCMsg.PullGroupMemberRequest()
        // set 
    pullGroupMemberRequest.setTarget(parm.target)
    pullGroupMemberRequest.setHead(parm.head)
        // 序列化
    let bytes = pullGroupMemberRequest.serializeBinary()
    return bytes
}

// 获取群成员回包
export const _pullGroupMemberResult = (data) => {
    // 反序列化
    let result = pbWFCMsg.PullGroupMemberResult.deserializeBinary(data)
    return result.getMemberList()
}

// 获取群成员member
export const _groupMemberRes = (data) => {
    let list = []
    data.map(item => {
        list.push({
            name: item.getAlias(),
            type: item.getType(),
            uid: item.getMemberId()
        })
    })
    return list
}

// groupmember
export const _groupMember = (uid) => {
    let groupMember = new pbWFCMsg.GroupMember()

    groupMember.setMemberId(uid)
    groupMember.setType(0)
    return groupMember
}

// 添加群成员member
export const _AddGroupMemberRequest = (parm) => {
    let AddGroupMemberRequest = new pbWFCMsg.AddGroupMemberRequest();
    AddGroupMemberRequest.setGroupId(parm.uid)
    AddGroupMemberRequest.setAddedMemberList(parm.list)

    let bytes = AddGroupMemberRequest.serializeBinary()
    return bytes

}

// 删除群成员member
export const _RemoveGroupMemberRequest = (parm) => {
    let RemoveGroupMemberRequest = new pbWFCMsg.RemoveGroupMemberRequest();

    RemoveGroupMemberRequest.setGroupId(parm.uid)
    RemoveGroupMemberRequest.setRemovedMemberList(parm.list)
    console.log(RemoveGroupMemberRequest)

    let bytes = RemoveGroupMemberRequest.serializeBinary()
    return bytes

}